package com.c.ctgproject.ui.collect

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityPartOutSubmitBinding
import com.c.ctgproject.entity.guang.BatteryCanHandlerEntity
import com.c.ctgproject.entity.guang.PartOutListEntity
import com.c.ctgproject.enum.DictionaryMenuEnum
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*

/**
 *作者:LiBW
 *创建日期: 2021/9/17
 *描述:零部件出库提交 页面
 */
class PartOutSubmitActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityPartOutSubmitBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val partsIdList = ArrayList<Int>()

    private val selectList by lazy { intent.getSerializableExtra(MyConstants.DATA) as ArrayList<PartOutListEntity.DataBean.ListBean> }

    override fun event() {
        selectList.forEach {
            partsIdList.add(it.partsId)
        }
    }

    var  customerName=""
    private var outType = 1

    override fun click() {
        mBinding.mXS.setOnClickListener { outType = 1 }
        mBinding.mJY.setOnClickListener { outType = 2 }
        mBinding.mQT.setOnClickListener { outType = 3 }
        //选择客户名称
        mBinding.mDictionarySelectLL.setOnClickListener {
            DictionaryMenuActivity.jumpToDictionary(this, DictionaryMenuEnum.CUSTOMER.menuCode, true, arrayListOf(customerName))
        }
        mBinding.mConfirmBTN.setOnClickListener {
            if (customerName.isEmpty()) {
                showToast("请填写客户名称")
            } else {
                val map = HashMap<String, Any>()
                map["customer"] = customerName
                map["outType"] = outType
                map["remark"] = mBinding.mRemarkET.text.handlerNull()
                map["partsIdList"] = partsIdList
                ApiMethods.apiSubscribe(ApiMethods.retrofit.partOutSubmit(handlerBody(map)), MyObserverHandlerLoading(this, loading) {
                    MyLog.d("回调:$it")
                    showToast("提交成功")
                    finish()
                })
            }
        }
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        data?.let { result ->
            //字典回调
            if (requestCode == 1000) {
                val name = result.getStringExtra(MyConstants.NAME)
                customerName = name.handlerNull()
                mBinding.mClientET.text = name.handlerNull()
            }

        }
    }
}